package com.xbongbong.paas.controller.mobile;/*/*/

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.message.pojo.dto.MessageCountDTO;
import com.xbongbong.pro.message.pojo.dto.MessageGroupDTO;
import com.xbongbong.pro.message.pojo.dto.MessageListDTO;
import com.xbongbong.pro.message.pojo.dto.MessageReadAllDTO;
import com.xbongbong.pro.message.pojo.dto.MessageReadDTO;
import com.xbongbong.pro.message.pojo.dto.MessageRemindListDTO;
import com.xbongbong.pro.message.pojo.dto.MessageSearchFilterDTO;
import com.xbongbong.pro.message.pojo.vo.MessageCountVO;
import com.xbongbong.pro.message.pojo.vo.MessageGroupVO;
import com.xbongbong.pro.message.pojo.vo.MessageReadAllVO;
import com.xbongbong.pro.message.pojo.vo.MessageReadVO;
import com.xbongbong.pro.message.pojo.vo.MessageSearchFilterVO;
import com.xbongbong.pro.message.pojo.vo.MessageVO;
import com.xbongbong.pro.message.service.MessageListService;
import com.xbongbong.pro.message.service.PushLinkService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * Description: 移动端消息列表
 * @author 魏荣杰
 * @date 2019/1/8 16:30
 * @version v1.0
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/mobile/message/search")
public class MessageSearchController {

    @Resource
    private MessageListService messageListService;
    @Resource
    private PushLinkService pushLinkService;

    @RequestMapping(value = "/index/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String indexList(@RequestBody @Valid MessageListDTO messageListDTO, BindingResult br) {
        XbbResponse<MessageVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MessageVO messageVO = messageListService.indexList(messageListDTO);
                response = new XbbResponse<>(messageVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 一键已读
     * @param messageReadAllDTO
     * @param br
     * @return java.lang.String
     * @throws
     */
    @RequestMapping(value = "/readAll", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String readAll(@RequestBody @Valid MessageReadAllDTO messageReadAllDTO, BindingResult br) {
        XbbResponse<MessageReadAllVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MessageReadAllVO messageReadAllVO = pushLinkService.readAllPushLink(messageReadAllDTO);
                response = new XbbResponse<>(messageReadAllVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 获取消息分组列表信息
     * @param messageGroupDTO
     * @param br
     * @return java.lang.String
     * @throws
     */
    @RequestMapping(value = "/groupList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String groupList(@RequestBody @Valid MessageGroupDTO messageGroupDTO, BindingResult br) {
        XbbResponse<MessageGroupVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MessageGroupVO messageGroupVO = messageListService.groupList(messageGroupDTO);
                response = new XbbResponse<>(messageGroupVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 获取具体分组内消息列表
     * @param messageListDTO
     * @param br
     * @return java.lang.String
     * @throws
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid MessageListDTO messageListDTO, BindingResult br) {
        XbbResponse<MessageVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MessageVO messageVO = messageListService.list(messageListDTO);
                response = new XbbResponse<>(messageVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 获取所有未读消息总数
     * @param messageCountDTO
     * @param br
     * @return java.lang.String
     * @throws
     */
    @RequestMapping(value = "/unreadCount", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String unreadCount(@RequestBody @Valid MessageCountDTO messageCountDTO, BindingResult br) {
        XbbResponse<MessageCountVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MessageCountVO messageCountVO = pushLinkService.countUnread(messageCountDTO);
                response = new XbbResponse<>(messageCountVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 获取不同分组内的筛选项
     * @param messageSearchFilterDTO
     * @param br
     * @return java.lang.String
     * @throws
     */
    @RequestMapping(value = "/searchFilter", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String searchFilter(@RequestBody @Valid MessageSearchFilterDTO messageSearchFilterDTO, BindingResult br) {
        XbbResponse<MessageSearchFilterVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MessageSearchFilterVO messageSearchFilterVO = messageListService.searchFilter(messageSearchFilterDTO);
                response = new XbbResponse<>(messageSearchFilterVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 单条消息已读
     * @param messageReadDTO
     * @param br
     * @return java.lang.String
     * @author 魏荣杰
     * @date 2019/1/16 10:24
     * @since v1.0
     */
    @RequestMapping(value = "/read", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String read(@RequestBody @Valid MessageReadDTO messageReadDTO, BindingResult br) {
        XbbResponse<MessageReadVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MessageReadVO messageReadVO = pushLinkService.readPushList(messageReadDTO);
                response = new XbbResponse<>(messageReadVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 首页看板：获取未读跟进提醒列表（只取2条）
     * @param messageRemindListDTO
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-03-26 16:51
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/indexRemindList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String indexRemindList(@RequestBody @Valid MessageRemindListDTO messageRemindListDTO, BindingResult br) {
        XbbResponse<MessageVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MessageVO messageVO = messageListService.indexRemindList(messageRemindListDTO);
                response = new XbbResponse<>(messageVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
